'use strict';
const {
	eachDocs,
	markets,
	setLineItem
} = require("lg-utils-db");
const {
	getTimingLogger
} = require("lg-utils");
const {
	confMap,
} = require("lg-constant");
const {
	getStockDWMKLine
} = require("lg-request-stock")
const db = uniCloud.database();
const dbCmd = db.command
const stockColl = db.collection("stock-base");
const stockDayLineColl = db.collection("stock-day-line");
const stockWeekLineColl = db.collection("stock-week-line");
const stockMonthLineColl = db.collection("stock-month-line");
const logTimingColl = db.collection("log-timing");
exports.main = async (event, context) => {
	
	try {
		const marketsInfo = await markets() || {};
		const {
			SH_info,
			SZ_info
		} = marketsInfo;
		if (SH_info == "已收盘" && SZ_info == "已收盘" ) {

			const {
				status,
				errorList,
				error,
				count
			} = await eachDocs(async ({
				symbol
			} = {}) => {
				return await setLineItem({
					type: "day",
					symbol
				});
			}, {
				coll: stockDayLineColl,
				fields: {
					name: true,
					symbol: true,
					code: true
				},
				pageSize: 100
			}) || {};

			if (status) {
				logTimingColl.add(getTimingLogger({
					action: "updateDayLine",
					info: `count:${count},errorList:${errorList}`,
				}))
			} else {
				logTimingColl.add(getTimingLogger({
					action: "updateDayLine",
					info: error,
					type: confMap.get("timing-error"),
				}))
			}

		} else {
			logTimingColl.add(getTimingLogger({
				action: "updateDayLine",
				info: marketsInfo,
				type: confMap.get("timing-common")
			}))
		}
	} catch (e) {
		logTimingColl.add(getTimingLogger({
			action: "updateDayLine",
			info: e,
			type: confMap.get("timing-exception")
		}))
	}
};
